package com.gzhryc.shared_device.oem.h5.agent.controllers.api.v1.pcl;

import com.gzhryc.common.ClassTools;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.device.EDeviceFunCode;
import com.gzhryc.shared_device.oem.code.device.dao.models.AgentRelationPCLDevice;
import com.gzhryc.shared_device.oem.code.device.services.AgentRelationPCLDeviceService;
import com.gzhryc.shared_device.oem.code.device.services.dto.AgentRelationPCLDeviceSearch;
import com.gzhryc.shared_device.oem.code.user.ERoleType;
import com.gzhryc.shared_device.oem.h5.agent.ClientAPICodeConstants;
import com.gzhryc.shared_device.oem.h5.agent.LoginUser;
import com.gzhryc.shared_device.oem.h5.agent.WebConstants;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.pcl.PCLDeviceCountResponse;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.pcl.PCLDeviceListRequest;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.pcl.PCLDeviceListResponse;
import com.gzhryc.shared_device.pcl.code.dao.db.ChargingCable;
import com.gzhryc.shared_device.pcl.code.dao.enums.ChargingCableEnum;
import com.gzhryc.shared_device.pcl.code.services.ChargingCableService;
import com.gzhryc.shared_device.pcl.code.services.dto.ChargingCableSearch;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class PCLDeviceController {

    @Route(value = "/agent/api/v1/pcl/device/count.data")
    public PCLDeviceCountResponse deviceCount(HttpServletRequest request){
        PCLDeviceCountResponse apiResp = new PCLDeviceCountResponse(ClientAPICodeConstants.NULL_DATA, "暂无数据");
        LoginUser loginUser = WebUtils.getAttribute(request, WebConstants.ATTR_LOGIN_USER, LoginUser.class);
        if (loginUser != null) {
            if(ERoleType.Agent.index().equals(loginUser.getRoleType())) {
                ChargingCableSearch search = new ChargingCableSearch();
                search.setUserId(loginUser.getId());

                Long deviceCount = ChargingCableService.self().countJoinBySearch(search);

                search.setDeploy(true);
                Long deployCount = ChargingCableService.self().countJoinBySearch(search);

                search.setDeploy(null);
                search.setState(ChargingCableEnum.State.Working.index());
                Long useCount = ChargingCableService.self().countJoinBySearch(search);

                apiResp.setCode(ClientAPICodeConstants.SUCCESS,null);
                apiResp.getBody().setDeviceFunCodeName(EDeviceFunCode.PCL.note());
                apiResp.getBody().setDeviceCount(deviceCount);
                apiResp.getBody().setDeployCount(deployCount);
                apiResp.getBody().setUseCount(useCount);
            }else{
                AgentRelationPCLDeviceSearch search = new AgentRelationPCLDeviceSearch();
                search.setUserId(loginUser.getParentId());
                search.setRelationUserId(loginUser.getId());

                Long deviceCount = AgentRelationPCLDeviceService.self().countJoinBySearch(search);

                search.setDeploy(true);
                Long deployCount = AgentRelationPCLDeviceService.self().countJoinBySearch(search);

                search.setDeploy(null);
                search.setState(ChargingCableEnum.State.Working.index());
                Long useCount = AgentRelationPCLDeviceService.self().countJoinBySearch(search);

                apiResp.setCode(ClientAPICodeConstants.SUCCESS,null);
                apiResp.getBody().setDeviceFunCodeName(EDeviceFunCode.PCL.note());
                apiResp.getBody().setDeviceCount(deviceCount);
                apiResp.getBody().setDeployCount(deployCount);
                apiResp.getBody().setUseCount(useCount);
            }
        }
        return apiResp;
    }

    @Route(value = "/agent/api/v1/pcl/device/list.data")
    public PCLDeviceListResponse list(HttpServletRequest request) {
        PCLDeviceListResponse apiResp = new PCLDeviceListResponse(ClientAPICodeConstants.NULL_DATA, "暂无数据");
        PCLDeviceListRequest apiReq = WebUtils.getJsonRequest(request, PCLDeviceListRequest.class);
        if (apiReq != null) {
            Integer pageNo = apiReq.getBody().getPageNo() == null ? 1 : apiReq.getBody().getPageNo();
            Integer deployState = apiReq.getBody().getDeployState() == null ? 0 : apiReq.getBody().getDeployState();
            LoginUser loginUser = WebUtils.getAttribute(request, WebConstants.ATTR_LOGIN_USER, LoginUser.class);
            if (loginUser != null) {
                List<ChargingCable> chargingCableList = null;
                if(ERoleType.Agent.index().equals(loginUser.getRoleType())) {
                    ChargingCableSearch search = new ChargingCableSearch();
                    search.setUserId(loginUser.getId());
                    if(apiReq.getBody().getState() != null && apiReq.getBody().getState() > 0) {
                        search.setState(apiReq.getBody().getState());
                    }
                    search.setTrim(apiReq.getBody().getTrim());
                    if (deployState == 1) {
                        search.setDeploy(false);
                    } else if (deployState == 2) {
                        search.setDeploy(true);
                    }
                    chargingCableList = ChargingCableService.self().findJoinBySearch(search, pageNo, WebConstants.PAGE_LIMIT);
                }else{
                    AgentRelationPCLDeviceSearch search = new AgentRelationPCLDeviceSearch();
                    search.setUserId(loginUser.getParentId());
                    if(apiReq.getBody().getState() != null && apiReq.getBody().getState() > 0) {
                        search.setState(apiReq.getBody().getState());
                    }
                    search.setRelationUserId(loginUser.getId());
                    search.setTrim(apiReq.getBody().getTrim());
                    if (deployState == 1) {
                        search.setDeploy(false);
                    } else if (deployState == 2) {
                        search.setDeploy(true);
                    }
                    List<AgentRelationPCLDevice> data = AgentRelationPCLDeviceService.self().findJoinBySearch(search,pageNo, WebConstants.PAGE_LIMIT);
                    if(data != null && data.size() > 0){
                        chargingCableList = new ArrayList<>();
                        for(AgentRelationPCLDevice item : data){
                            chargingCableList.add(item);
                        }
                    }
                }

                if (chargingCableList != null && chargingCableList.size() > 0) {
                    apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
                    apiResp.getBody().setItems(new ArrayList<>());

                    for (ChargingCable chargingCable: chargingCableList) {
                        PCLDeviceListResponse.DeviceItem item = ClassTools.copy(new PCLDeviceListResponse.DeviceItem(), chargingCable);
                        item.setStateName(chargingCable.getStateName());
                        item.setPlaceId(chargingCable.getDevice().getPlaceId());
                        item.setPlaceName(chargingCable.getDevice().getPlaceName());
                        item.setPlaceAreaName(chargingCable.getDevice().getAreaName());
                        apiResp.getBody().getItems().add(item);
                    }
                }
            }
        }
        return apiResp;
    }
}
